package com.cn.kabalifeuser.ui.service.activity;

import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.bean.UpImgBean;
import com.cn.kabalifeuser.ui.service.bean.RecruitDetailsBean;
import com.cn.kabalifeuser.ui.service.bean.RecruitListBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;
import java.util.List;

/**
 * Created by 晓林 on 2020/9/11.
 * function:
 */
public class RecruitListPresenter extends BasePresenter<RecruitListView> {
    /**
     * 招聘列表
     *
     * @param token
     * @param page
     * @param limit
     * @param id
     */
    public void onRecruitListData(String token, int page, int limit, int id) {
        OkGo.<RecruitListBean>get(Api.API_RECRUIT_LIST)
                .headers("token", token)
                .params("type", 1)
                .params("page", page)
                .params("limit", limit)
                .params("business_id", id)
                .execute(new CacheCallBack<RecruitListBean>() {
                    @Override
                    public void onSuccess(Response<RecruitListBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("商家详情：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onRecruitListSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    public void onRecruitDetailsData(String token, int id) {
        OkGo.<RecruitDetailsBean>get(Api.API_RECRUIT_DETAILS)
                .headers("token", token)
                .params("id", id)
                .execute(new CacheCallBack<RecruitDetailsBean>() {
                    @Override
                    public void onSuccess(Response<RecruitDetailsBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("详情：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onRecruitDetailsSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 上传文件。
     */
    public void onUpImgData(String img) {
        PostRequest postRequest = OkGo.<UpImgBean>post(Api.API_UP_IMG);
        postRequest.params("type", "head");
        postRequest.params("file[]", new File(img));
        postRequest.execute(new CacheCallBack<UpImgBean>(getView()._getContext(), true) {
            @Override
            public void onSuccess(Response<UpImgBean> response) {
                super.onSuccess(response);
                LoggerUtils.d("上传图:" + response.body());
                if (isViewAttached())
                    if (response.body().getCode() == Constants.HTTP_SUCCESS) {
                        getView().onUpImgSuccess(response.body());
                    } else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN) {
                        getView().onReLoggedIn(response.body().getMsg());
                    } else {
                        getView().onError(response.body().getMsg());
                    }

            }
        });
    }

    public void onAddMsgData(String token, int id, String name, String phone, List<String> img) {
        OkGo.<MsgBean>post(Api.API_RECRUIT_MSG)
                .headers("token", token)
                .params("id", id)
                .params("name", name)
                .params("phone", phone)
                .addUrlParams("img[]", img)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("留言" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAddMsgSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }
}
